define(['zepto', 'butterfly/view','../../bsl',
    'text!./statistic.html'
], function($, View, bsl, statistic) {
    var me = null;
    var duration=null;
    var counter=null;
    var loading=null;

    var view = View.extend({
        events: {
           "tap #back":"back",
           "tap #statisticBack":"statisticBack",
           "tap #duration":"doCounter",
           "tap #openDoc":"OpenDoc"
        },
        render: function() {
            me = this;

        },

        onShow: function() {

            
            document.addEventListener( "plusready", function(){
            counter = document.getElementById("counter");
            loading = document.getElementById("loading");
            duration = document.getElementById("duration");
            duration.addEventListener( "touchstart", durationStart, false );
            duration.addEventListener( "touchend", durationEnd, false );
            bsl.statistic.eventTrig( "open" );
            bsl.statistic.eventStart( "stay" );
        }, false );         


        },
        
        // statisticBack: function() {
        //     back(true);
        //     bsl.statistic.eventEnd( "stay" );
        // },

        tc:null,
        tl:null,
        count:0,
        ds:null,

        doCounter: function(){
            loading.style.webkitTransition = "all 0.8s ease-in-out";
            loading.style.borderWidth = "0";
            loading.style.borderColor = "rgba(128,128,128,0.8)";
            me.tc = setTimeout( function(){
                me.count++;
                loading.style.webkitTransition = "";
                loading.style.borderWidth = "50px";
                loading.style.borderColor = "rgba(255,255,255,0)";
                counter.innerText = me.count;
                me.tl=setTimeout( doCounter, 0 );
            }, 1000 );
        },

        durationStart: function(){
            me.ds = new Date();
            me.count = 0;
            counter.innerText = "0";
            counter.style.visibility = "visible";
            doCounter();
            console.info( "按下开始计时：" );
        },

        durationEnd: function(){
            clearTimeout( tc );
            me.tc = null;
            clearTimeout( tl );
            me.tl = null;
            loading.style.webkitTransition = "";
            loading.style.borderWidth = "50px";
            loading.style.borderColor = "rgba(255,255,255,0)";
            counter.style.visibility = "hidden";
            var d = (new Date()).getTime()-ds.getTime();
            bsl.statistic.eventDuration( "press", d );
            console.info( "您按下的时间为"+d+"ms，后台会将此数据提交到服务器！" );
        },

        back : function(e){
            window.history.go(-1);
        },

        OpenDoc : function(e){
           
            window.butterfly.navigate('com.no5.cordovademo/doc/statistic', {
                trigger: true
            });
        },
        
    });
    return view;
});
